<template>
  <div class="container">
    <Sidebar 
      :visible="uiStore.sidebarOpen" 
      :logs="robotStore.logs" 
      :isConnected="robotStore.isConnected"
      :temperature="robotStore.temperature"
      :humidity="robotStore.humidity"
      @close="uiStore.closeSidebar()" 
      @clear-log="robotStore.clearLogs()" 
    />
    <header class="header">
      <button class="menu-btn" @click="uiStore.toggleSidebar()">☰</button>
      <h1><img src="@/assets/images/Rang_logo.png" alt="润泽机器人" class="robot-logo"> 润泽机器人控制中心</h1>
      <div class="connection-status">
        <span>{{ robotStore.isConnected ? '已连接' : '未连接' }}</span>
        <div class="status-indicator" :class="{ connected: robotStore.isConnected }"></div>
      </div>
      <div class="battery-status">
        <div class="battery-icon" :class="robotStore.batteryLevelClass">
          <div class="battery-level" :style="{ width: robotStore.batteryPercentage + '%' }"></div>
        </div>
        <span class="battery-text">{{ robotStore.isConnected ? (robotStore.batteryPercentage.toFixed(0) + '%') : '-  -' }}</span>
      </div>
    </header>
    <router-view
      :ros="robotStore.ros"
      :robotIp="robotStore.robotIp"
      :robotPort="robotStore.robotPort"
      :isConnected="robotStore.isConnected"
      :linearSpeed="robotStore.linearSpeed"
      :angularSpeed="robotStore.angularSpeed"
      :rosConnection="rosConnection"
      @update:robotIp="updateRobotIp"
      @update:robotPort="updateRobotPort"
      @connect="rosConnection.connect"
      @disconnect="rosConnection.disconnect"
      @setup-camera-canvas="rosConnection.setupCameraCanvas"
    />
  </div>
</template>

<script>
import Sidebar from './components/Sidebar.vue';
import { useRobotStore } from '@/stores/robotStore';
import { useUIStore } from '@/stores/uiStore';
import { useROSConnection } from '@/composables/useROSConnection';
import { useRobotConfig } from '@/composables/useRobotConfig';

export default {
  name: 'App',
  components: { Sidebar },
  
  setup() {
    // 状态管理
    const robotStore = useRobotStore();
    const uiStore = useUIStore();
    
    // 功能模块
    const rosConnection = useROSConnection();
    const robotConfig = useRobotConfig();

    return {
      // 状态管理
      robotStore,
      uiStore,
      
      // 功能模块
      rosConnection,
      robotConfig,
      
      // 机器人配置方法
      updateRobotIp: robotConfig.updateRobotIp,
      updateRobotPort: robotConfig.updateRobotPort
    };
  }
};
</script>

<style>
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
body {
  font-family: 'Microsoft YaHei', Arial, sans-serif;
  background: linear-gradient(135deg, #1e3c72, #2a5298, #1e3c72);
  color: white;
  overflow-x: hidden;
  min-height: 100vh;
  user-select: none;
}
.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding: 20px;
  margin: 16 auto;
}

/* 头部样式 */
.header {
    text-align: center;
    margin-bottom: 30px;
    background: rgba(0, 0, 0, 0.3);
    padding: 20px;
    border-radius: 15px;
    backdrop-filter: blur(10px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
    position: relative;
}

.header h1 {
    font-size: 2.5rem;
    margin-bottom: 15px;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.robot-logo {
    width: 40px;
    height: 40px;
    object-fit: contain;
    filter: drop-shadow(0 0 5px rgba(255, 255, 255, 0.3));
}

/* 电量显示样式 */
.battery-status {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    gap: 8px;
    z-index: 100;
}

.battery-icon {
    width: 30px;
    height: 15px;
    border: 2px solid #fff;
    border-radius: 3px;
    position: relative;
    background: rgba(255, 255, 255, 0.1);
}

.battery-icon::after {
    content: '';
    position: absolute;
    right: -4px;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 6px;
    background: #fff;
    border-radius: 0 2px 2px 0;
}

.battery-level {
    height: 100%;
    background: #2ed573;
    border-radius: 1px;
    transition: all 0.3s ease;
}

.battery-icon.low .battery-level {
    background: #ffa502;
    animation: pulse 2s infinite;
}

.battery-icon.critical .battery-level {
    background: #ff4757;
    animation: pulse 1s infinite;
}

.battery-text {
    font-weight: bold;
    color: #fff;
    font-size: 0.8rem;
}

/* PC端导航按钮样式 */
.menu-btn {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 1100;
  background: none;
  border: none;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
  padding: 0 6px;
  line-height: 1;
  transition: color 0.2s;
}

.menu-btn:hover {
  color: #2ed573;
}

.connection-status {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-size: 14px; /* 基准字号 */
}

/* 调整“已连接/未连接”文字大小 */
.connection-status span {
    font-size: 14px !important; /* 提高优先级，避免被继承覆盖 */
    line-height: 1.1;
}

/* 平板断点 */
@media (max-width: 1024px) {
  .connection-status span { font-size: 13px !important; }
}

/* 手机断点 */
@media (max-width: 480px) {
  .connection-status span { font-size: 16px !important; }
}

.status-indicator {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #ff4757;
    box-shadow: 0 0 10px #ff4757;
    animation: pulse 2s infinite;
}

.status-indicator.connected {
    background: #2ed573;
    box-shadow: 0 0 10px #2ed573;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.2); opacity: 0.7; }
}


/* 控制面板 */
.control-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 30px;
}

.connection-panel {
    background: rgba(0, 0, 0, 0.4);
    padding: 20px;
    border-radius: 15px;
    backdrop-filter: blur(10px);
}

.input-group {
    margin-bottom: 15px;
}

.input-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
}

.input-group input {
    width: 100%;
    padding: 10px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 1rem;
}

.input-group input::placeholder {
    color: rgba(255, 255, 255, 0.6);
}

.connect-btn, .disconnect-btn {
    padding: 12px 25px;
    border: none;
    border-radius: 8px;
    font-size: 1rem;
    font-weight: bold;
    cursor: pointer;
    margin-right: 10px;
    transition: all 0.3s ease;
}

.connect-btn {
    background: linear-gradient(45deg, #2ed573, #17a2b8);
    color: white;
}

.connect-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(46, 213, 115, 0.4);
}

.disconnect-btn {
    background: linear-gradient(45deg, #ff4757, #ff6b7a);
    color: white;
}

.disconnect-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(255, 71, 87, 0.4);
}

.disconnect-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* 速度显示 */
.speed-display {
    background: rgba(0, 0, 0, 0.4);
    padding: 20px;
    border-radius: 15px;
    backdrop-filter: blur(10px);
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.speed-item {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.2rem;
}

.speed-label {
    font-weight: bold;
    min-width: 80px;
}

.speed-value {
    font-family: 'Courier New', monospace;
    font-size: 1.5rem;
    color: #2ed573;
    text-shadow: 0 0 5px rgba(46, 213, 115, 0.5);
}

.speed-unit {
    color: rgba(255, 255, 255, 0.7);
    font-size: 1rem;
}

/* 摇杆区域布局 - 具体样式由各组件自己处理 */
.joystick-area {
    display: flex;
    justify-content: space-around;
    align-items: center;
    gap: 50px;
    margin: 50px 0;
    padding: 40px 0;
}

.joystick-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
}

.joystick-label {
    font-size: 1.3rem;
    font-weight: bold;
    text-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
    margin-bottom: 10px;
}

/* 控制按钮区域 */
.button-area {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin: 40px 0;
    flex-wrap: wrap;
}

.control-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 1rem;
    font-weight: bold;
    color: white;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
}

.emergency-stop {
    background: radial-gradient(circle at 30% 30%, #ff4757, #c44569);
}

.emergency-stop:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 25px rgba(255, 71, 87, 0.4);
}

.speed-boost {
    background: radial-gradient(circle at 30% 30%, #ffa502, #ff6348);
}

.speed-boost:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 25px rgba(255, 165, 2, 0.4);
}

.speed-boost.active {
    background: radial-gradient(circle at 30% 30%, #2ed573, #17a2b8);
    animation: glow 1s ease-in-out infinite alternate;
}

@keyframes glow {
    from { box-shadow: 0 0 20px rgba(46, 213, 115, 0.5); }
    to { box-shadow: 0 0 30px rgba(46, 213, 115, 0.8); }
}

.reset-position {
    background: radial-gradient(circle at 30% 30%, #3742fa, #2f3542);
}

.reset-position:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 25px rgba(55, 66, 250, 0.4);
}

.btn-icon {
    font-size: 2rem;
    margin-bottom: 5px;
}

.btn-text {
    font-size: 0.9rem;
}

/* 日志区域 */
.log-area {
    background: rgba(0, 0, 0, 0.4);
    border-radius: 15px;
    overflow: hidden;
    backdrop-filter: blur(10px);
    margin-top: 30px;
}

.log-header {
    background: rgba(0, 0, 0, 0.5);
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: bold;
}

.clear-log-btn {
    background: rgba(255, 71, 87, 0.8);
    color: white;
    border: none;
    padding: 8px 15px;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: all 0.3s ease;
}

.clear-log-btn:hover {
    background: rgba(255, 71, 87, 1);
    transform: translateY(-2px);
}

.log-content {
    max-height: 200px;
    overflow-y: auto;
    padding: 15px 20px;
}

.log-entry {
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 0.9rem;
    color: rgba(255, 255, 255, 0.8);
}

.log-entry:last-child {
    border-bottom: none;
}

.log-entry.success {
    color: #2ed573;
}

.log-entry.error {
    color: #ff4757;
}

.log-entry.warning {
    color: #ffa502;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .container {
    padding: 10px;
  }
  .joystick-area {
    gap: 20px;
    padding: 20px 0;
  }
}
@media (max-width: 768px) {
  .container {
    padding: 5px;
  }
  .header h1 {
    font-size: 1.5rem;
  }
  .battery-status {
    right: 10px;
    top: 5px;
    transform: none;
    gap: 6px;
    position: fixed;
  }
  .battery-icon {
    width: 15px;
    height: 10px;
  }
  .battery-text {
    font-size: 0.7rem;
  }
  .control-panel {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .joystick-area {
    flex-direction: column;
    gap: 20px;
    margin: 20px 0;
    padding: 10px 0;
  }
  .button-area {
    gap: 10px;
  }
  .control-btn {
    width: 70px;
    height: 70px;
    font-size: 0.9rem;
  }
  .btn-icon {
    font-size: 1.2rem;
  }
  .input-group input {
    font-size: 1rem;
    padding: 10px;
  }
  .connect-btn, .disconnect-btn {
    font-size: 1rem;
    padding: 10px 18px;
  }
  .menu-btn {
    position: absolute;
    left: 10px;
    top: 10px;
    z-index: 1100;
    background: none;
    border: none;
    color: #fff;
    font-size: 2rem;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    transition: color 0.2s;
  }
  .menu-btn:hover {
    color: #2ed573;
  }
}
@media (max-width: 480px) {
  .header {
    padding: 8px !important;
    margin-bottom: 10px !important;
    border-radius: 0px !important;
    /* background: linear-gradient(135deg, #2ed573 0%, #17a2b8 100%) !important;
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
    backdrop-filter: none !important; */
  }
  .header h1 {
    font-size: 1.2rem;
    margin-bottom: 8px;
    gap: 6px;
  }
  .robot-logo {
    width: 24px;
    height: 24px;
  }
  .battery-status {
    right: 8px !important;
    top: 8px !important;
    gap: 4px !important;
  }
  .battery-icon {
    width: 16px !important;
    height: 8px !important;
  }
  .battery-text {
    font-size: 0.7rem !important;
  }
  .container {
    padding: 2px;
    /* margin-bottom: 100px; */
  }
  .control-panel {
    grid-template-columns: 1fr;
    gap: 2px;
    margin-bottom: 2px;
    border-radius: 8px;
    padding: 4px;
  }
  .speed-display {
    padding: 4px;
    border-radius: 8px;
    gap: 4px;
    margin-top: 0;
  }
  .speed-item {
    font-size: 1rem;
    gap: 6px;
  }
  .joystick-area {
    flex-direction: column;
    gap: 12px;
    margin: 8px 0;
    padding: 4px 0;
    align-items: flex-start;  /* 让摇杆靠左对齐 */
    padding-left: 60px;       /* 左边留一些间距 */
  }
  .joystick-label {
    font-size: 1rem;
    margin-bottom: 6px;
  }
  .button-area {
    gap: 8px;
    margin: 12px 0;
  }
  .control-btn {
    width: 44px;
    height: 44px;
    font-size: 0.8rem;
    border-radius: 50%;
  }
  .btn-icon {
    font-size: 1rem;
    margin-bottom: 2px;
  }
  .btn-text {
    font-size: 0.7rem;
  }
  .input-group input {
    font-size: 0.9rem;
    padding: 6px;
  }
  .connect-btn, .disconnect-btn {
    font-size: 0.9rem;
    padding: 6px 10px;
  }
  .log-content {
    padding: 6px 3px;
    font-size: 0.7rem;
  }
  .menu-btn {
    position: absolute;
    left: 10px;
    top: 10px;
    z-index: 1100;
    background: none;
    border: none;
    color: #fff;
    font-size: 1.6rem;
    cursor: pointer;
    padding: 0 1px;
    line-height: 1;
    transition: color 0.2s;
  }
  .menu-btn:hover {
    color: #2ed573;
  }
}

/* 滚动条样式 */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5);
}
</style>
